<?php

namespace App\Entity\Sale;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Repository\Sale\PromotionCategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=PromotionCategoryRepository::class)
 */
class PromotionCategory
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @Groups({"sale"})
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Common(cnName="名称",listType="text",editType="text",width="200")
     * @Groups({"sale"})
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @Common(cnName="活动",listType="text",editType="text",width="200")
     * @Groups({"sale"})
     * @ORM\ManyToOne(targetEntity=Promotion::class, inversedBy="categories")
     * @ORM\JoinColumn(nullable=false)
     */
    private $promotion;

    /**
     * @Common(cnName="产品列表",listType="text",editType="text")
     * @Groups({"sale"})
     * @ORM\OneToMany(targetEntity=PromotionProduct::class, mappedBy="category", orphanRemoval=true)
     */
    private $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getPromotion(): ?Promotion
    {
        return $this->promotion;
    }

    public function setPromotion(?Promotion $promotion): self
    {
        $this->promotion = $promotion;

        return $this;
    }

    /**
     * @return Collection|PromotionProduct[]
     */
    public function getProducts(): Collection
    {
        return $this->products;
    }

    public function addProduct(PromotionProduct $product): self
    {
        if (!$this->products->contains($product)) {
            $this->products[] = $product;
            $product->setCategory($this);
        }

        return $this;
    }

    public function removeProduct(PromotionProduct $product): self
    {
        if ($this->products->contains($product)) {
            $this->products->removeElement($product);
            // set the owning side to null (unless already changed)
            if ($product->getCategory() === $this) {
                $product->setCategory(null);
            }
        }

        return $this;
    }
}
